<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>823680621</title>
	<style>
		a{margin: 20px;}
	</style>
</head>
<body>
	<a id="del" href="#">删除canvas</a>
	<a id="sc" href="#">生成canvas图片</a>
</body>
<script>
	var cvs=document.createElement("canvas");
	cvs.style.body="2px solid red";
	var ctx =cvs.getContext("2d");
	ctx.fillStyle="yellow";
	ctx.fillRect(0,0,300,150);
	ctx.fillStyle="blue";
	ctx.fillRect(50,50,100,150);
	del.onclick=function(){
		document.body.removeChild(cvs)
	}
	document.body.appendChild(cvs);

	//把canvas转换成Blob
	cvs.toBlob(function(blob){
		console.log(blob);
		var reader=new FileReader();
		reader.readAsDataURL(blob,'utf-8');
		console.log(blob)
		reader.onload=function(e){
			console.log(this.result);
			var a=document.createElement("a");
		 	a.innerHTML="点击下载";
		 	a.href=window.URL.createObjectURL(blob);
		 	document.body.appendChild(a);
		 	a.onclick=function(){
 			a.download='canvas.png';//下载文件，doc文件
 	}
 	///点击生成一张图片
 		sc.onclick=function(){
 		var img=document.createElement("img");
 		img.src=window.URL.createObjectURL(blob);
 		 document.body.appendChild(img);
 		}
 		///
		}
	}) 
</script>
</html>